package com.xl.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.generator.common.ActionResult;
import com.xl.generator.common.ActionResultService;
import com.xl.generator.domain.CommonUser;
import com.xl.generator.pojo.PageList;
import com.xl.generator.service.CommonUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈〉<br>
 *
 * @author lvln5
 * @Description
 * @date 2022/11/12 19:39
 */
@Api(value = "用户的增删改查")
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
	private CommonUserService commonUserService;
	@ApiOperation(value = "分页查询所有用户", notes="登录成功状态码为o,否侧为-1用户不存在")
	@PostMapping ("list")
	public ActionResult test(@RequestParam("current") Integer current,@RequestParam("Size") Integer size){
		IPage<CommonUser> commonUserIPage = new Page<>();
		commonUserIPage.setCurrent(current);
		commonUserIPage.setSize(size);
		List list = commonUserService.listALL(commonUserIPage);
		PageList pageList = new PageList();
		pageList.setTotal(commonUserIPage.getTotal());
		pageList.setData(list);
		return ActionResultService.callBackResultOK(pageList);
	}

	/**
	 * @param commonUser 登录
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "用户登录", notes="登录成功状态码为o,否侧为-1用户不存在")
	@PostMapping("login")
	public ActionResult login(@RequestBody CommonUser commonUser){
		LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CommonUser::getUserAccount,commonUser.getUserAccount()).eq(CommonUser::getPassword,commonUser.getPassword());
		CommonUser one = commonUserService.getOne(queryWrapper);
		if (one!=null){
			return ActionResultService.callBackResultOK("登录成功",one);
		}
		return ActionResultService.callBackResultFail("登录失败");
	}

	/**
	 * @param commonUser 注册
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "用户注册", notes="注册成功状态码为o,否侧为-1为用户存在")
	@PostMapping("register")
	public ActionResult register(@RequestBody CommonUser commonUser){
		System.out.println(commonUser.toString());
		LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CommonUser::getUserAccount,commonUser.getUserAccount());
		CommonUser one = commonUserService.getOne(queryWrapper);
		if (one!=null){
			//用户存在
			return ActionResultService.callBackResultFail("用户存在");
		}
		//否则不在插入
		if (commonUser.getRoleId()==null||commonUser.getRoleId()==1){
			commonUser.setDegree(1);
		}else {
			commonUser.setDegree(5);
		}
		boolean save = commonUserService.save(commonUser);
		if (save){
			return ActionResultService.callBackResultOK("注册成功");
		}
		return ActionResultService.callBackResultOK("注册失败");
	}
	/**
	 * @param commonUser 删除
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "删除", notes="删除成功状态码为o,否侧为-1失败")
	@PostMapping("del")
	public ActionResult del(@RequestBody CommonUser commonUser){
		LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CommonUser::getUserName,commonUser.getUserName());
		boolean remove = commonUserService.remove(queryWrapper);
		if (remove){
			return	ActionResultService.callBackResultOK("删除成功");
		}
		return ActionResultService.callBackResultFail("删除失败");
	}
	@ApiOperation(value = "更新", notes="更新成功状态码为o,否侧为-1失败")
	@PostMapping("update")
	public ActionResult updata(@RequestBody CommonUser commonUser){
//		LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.eq(CommonUser::getUserName,commonUser.getUserName());
		boolean update = commonUserService.updateById(commonUser);
		System.out.println(update);
		if (update){
		return 	ActionResultService.callBackResultOK("更新成功");
		}
		return ActionResultService.callBackResultFail("更新失败");
	}
}
